package formulation;

import java.util.ArrayList;
import java.util.List;

import es.deusto.ingenieria.is.csp.formulation.Constraint;
import es.deusto.ingenieria.is.csp.formulation.Variable;

public class SumConstraint extends Constraint<Integer>
{
	private List<Variable<Integer>> left = null;
	private List<Variable<Integer>> right = null;
	
	public SumConstraint(String name, List<Variable<Integer>> left, List<Variable<Integer>> right)
	{
		super(name, left);	
		
		this.left = left;
		this.right = right;
	}

	@Override
	public boolean isSatisfied(Variable<Integer> variable, Integer value)
	{
		int sumaIzquierda = 0;
		
		for (Variable<Integer> varAux : this.left) 
		{
			if (!varAux.equals(variable) && !varAux.hasValue())
			{
				return true;
			}
			else if (varAux.equals(variable))
			{
				sumaIzquierda += value;
			}
			else if (varAux.hasValue())
			{
				sumaIzquierda += varAux.getValue();
			}
		}
		
		int sumaDerecha = 0;
		
		for (Variable<Integer> varAux : this.right) 
		{
			if (!varAux.equals(variable) && !varAux.hasValue())
			{
				return true;
			}
			else if (varAux.equals(variable))
			{
				sumaDerecha += value;
			}
			else if (varAux.hasValue())
			{
				if (varAux.getName().length() == 1)
				{
					sumaDerecha += varAux.getValue();
				}
				else
				{
					sumaDerecha += varAux.getValue() * 10;
				}
			}
		}
		
		return sumaIzquierda == sumaDerecha;
	}
	
	@Override
	public String toString()
	{	
		return this.getName() + ": " + this.getVariables().toString();
	}
	
	public void bindVariables()
	{
		this.addVariables((ArrayList<Variable<Integer>>) left);
		this.addVariables((ArrayList<Variable<Integer>>) right);
	}
	
	public List<Variable<Integer>> getLeft()
	{
		return left;
	}

	public void setLeft(List<Variable<Integer>> left)
	{
		this.left = left;
	}

	public List<Variable<Integer>> getRight()
	{
		return right;
	}

	public void setRight(List<Variable<Integer>> right)
	{
		this.right = right;
	}
	
//	public static void main(String[] args)
//	{
//		ArrayList<Integer> domain = new ArrayList<Integer>();
//		for (int i = 1; i < 10; i++)
//		{
//			domain.add(i);
//		}
//		domain.add(0);
//		
//		ArrayList<Variable<Integer>> left = new ArrayList<Variable<Integer>>();
//		
//		left.add(new Letra("A", domain));
//		left.get(0).setValue(1);
//		
//		left.add(new Letra("B", domain));
//		left.get(1).setValue(3);
//		
//		ArrayList<Variable<Integer>> right = new ArrayList<Variable<Integer>>();
//		
//		right.add(new Letra("C", domain));
//		right.get(0).setValue(1);
//		
//		right.add(new Llevada("X1", domain));
//		right.get(1).setValue(2);
//		
//		right.add(new Letra("D", domain));
//		right.get(2).setValue(5);
//		
//		SumConstraint sumConstraint = new SumConstraint("SumConstraint", left, right);	
//		System.out.println("SumConstraints: " + sumConstraint.isSatisfied(new Letra("A", domain), 23));
//	}
}
